Imports System.ComponentModel
Imports System.IO
Imports System.Reflection

Imports Phoenix.Forms.View
Imports Phoenix.Preferences

Namespace Forms

    ''' <summary>
    ''' The Phoenix main form. The application startup form.
    ''' </summary>
    Public Class MainForm : Inherits PhoenixFormBase

#Region " Windows Form Designer generated code "

        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call
        End Sub

        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents Timer As System.Timers.Timer
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(MainForm))
            Me.Timer = New System.Timers.Timer
            CType(Me.Timer, System.ComponentModel.ISupportInitialize).BeginInit()
            '
            'Timer
            '
            Me.Timer.Enabled = True
            Me.Timer.Interval = 1000
            Me.Timer.SynchronizingObject = Me
            '
            'MainForm
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(808, 502)
            Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
            Me.KeyPreview = True
            Me.MinimumSize = New System.Drawing.Size(700, 500)
            Me.Name = "MainForm"
            Me.Text = "Phoenix iTill System"
            CType(Me.Timer, System.ComponentModel.ISupportInitialize).EndInit()

        End Sub

#End Region

#Region " Form Events Handlers "
        Protected Overrides Sub OnLoad(ByVal e As EventArgs)
            MyBase.OnLoad(e)

            WindowState = FormWindowState.Maximized
        End Sub

        Private Sub Form_Closed(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Closed
            'AppExit()
        End Sub

        Private Sub Form_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
            If (e.KeyCode = Keys.F8) Then
                If TypeOf ActiveView Is LoginView Then
                    CType(ActiveView, LoginView).SafeMode()
                End If
            ElseIf (e.KeyCode = Keys.Escape) Then
                If TypeOf ActiveView Is MainView Then
                    AskAndLogOff()
                End If
            End If
        End Sub

#End Region

#Region " View Behaviour "
        Public ReadOnly Property ActiveView() As ViewBase
            Get
                Return mActiveView
            End Get
        End Property

        Public Sub CreateView(ByVal type As Type)
            If (type Is GetType(LoginView)) Or (Security.CurrentUser Is Nothing) Then
                Text = Res.ApplicationTitle '& " (" & [Assembly].GetExecutingAssembly().GetName().Version.ToString() & ")"
            Else
                Text = Res.ApplicationTitle & " - " & Security.CurrentUser.FullName '& " (" & [Assembly].GetExecutingAssembly().GetName().Version.ToString() & ")"
            End If

            AppCursor.SetBusy()
            SuspendLayout()
            Dim v As ViewBase = CType(Activator.CreateInstance(type), ViewBase)
            AddHandler v.ViewCreated, AddressOf NewViewCreated
            Controls.Add(v)
        End Sub

#End Region

#Region " View Implementation "
        Private WithEvents mActiveView As ViewBase

        Private Sub NewViewCreated(ByVal sender As Object, ByVal e As EventArgs)
            DestroyView(mActiveView)
            mActiveView = CType(sender, ViewBase)
            HideSplash()
            ResumeLayout()
            AppCursor.SetFree()
        End Sub

        Private Sub DestroyView(ByVal view As ViewBase)
            If view Is Nothing Then Return

            Controls.Remove(view)
            view.DestroyView()

            RemoveHandler view.ViewCreated, AddressOf NewViewCreated
        End Sub

        Private Sub AskAndLogOff()
            If AppMessageBox(Res.AskLogOut, MsgBoxStyle.YesNo Or MsgBoxStyle.Question Or MsgBoxStyle.DefaultButton1) = MsgBoxResult.Yes Then
                AppLogout()
            End If
        End Sub

#End Region

#Region " Timer Routines "
        Private Sub Timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer.Elapsed
            If TypeOf ActiveView Is MainView Then
                If AppConfig.EnableAutoLogout Then
                    Dim mins As Integer = Math.Floor(Now.Subtract(AppEventListener.LastWindowEventTime).TotalMinutes)
                    If mins >= AppConfig.AutoLogoutTime Then
                        AppLogout()
                    End If
                End If
            End If
        End Sub

#End Region

    End Class


End Namespace